<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="子设备型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入子设备型号"
          clearable
        />
      </el-form-item>
      <el-form-item label="子设备类型" prop="classify">
        <el-select v-model="form.classify" filterable placeholder="请选择">
          <el-option
            v-for="item in subDevice_classify"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="型号编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入型号编号"
          clearable
        />
      </el-form-item>
      <el-form-item label="型号名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入型号名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['genius:platform-device-model:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['genius:platform-device-model:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['genius:platform-device-model:removed']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['genius:platform-device-model:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getSubDeviceModelList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="subDeviceModelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="子设备型号" align="center" prop="model" :show-overflow-tooltip="true" />
      <el-table-column label="子设备类型" align="center" prop="classify" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
            <div v-for="item in subDevice_classify" :key="item.value">
              <div v-if="scope.row.classify === item.value">
                  {{ item.label }}
              </div>
            </div>
        </template>
      </el-table-column>
      <el-table-column label="型号编号" align="center" prop="code" :show-overflow-tooltip="true" />
      <el-table-column label="型号名称" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="地址" align="center" prop="address" :show-overflow-tooltip="true" />
      <el-table-column label="抓拍角度" align="center" prop="paimianAngle" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
            <div v-if="scope.row.classify != 1" >
                {{ scope.row.paimianAngle}}
            </div>
            <div style="color: blueviolet;" v-else >
                {{ "不需要角度"}}
            </div>
        </template>
      </el-table-column>
      <el-table-column label="使用的端口" align="center" prop="port" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <div v-if="scope.row.port != null" >
            <div v-for="item in subDevice_port" :key="item.value">
              <div v-if="scope.row.port === item.value">
                  {{ item.label }}
              </div>    
            </div>
          </div>
          <div style="color: blueviolet;" v-else >
              {{ "未配置端口"}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="方向" align="center" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <div v-if="scope.row.classify != 1" >
            {{ subDevice_camera_direction[scope.row.direction].label }}
          </div>
          <div style="color: blueviolet;" v-else >
              {{ "不需要方向"}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          {{ parseTime(new Date(scope.row.createTime)) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['genius:platform-device-model:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['genius:platform-device-model:removed']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getSubDeviceModelList"
    />

    <!-- 添加或修改子设备型号对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="子设备型号" prop="model">
              <el-input v-model="form.model" placeholder="请输入" maxlength="20" show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="子设备类型" prop="classify">
              <el-select v-model="form.classify" filterable placeholder="请选择">
                <el-option
                  v-for="item in subDevice_classify"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- <el-form-item label="型号编号" prop="code">
              <el-input v-model="form.code" placeholder="用户自定义数字" type="number" maxlength="4" show-word-limit/>
            </el-form-item> -->
            <el-form-item v-if="form.id == null" label="子设备型号编号" prop="code">
              <el-input v-model="form.code" placeholder="请输入子设备型号编号"  maxlength="30" show-word-limit/>
            </el-form-item>

            <el-form-item v-else label="子设备型号编号" prop="code">
              <el-input disabled v-model="form.code" placeholder="请输入子设备型号编号"  maxlength="30" show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="型号名称" prop="name">
              <el-input v-model="form.name" placeholder="请命名" maxlength="20" show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="ip或485 片选" type="number" maxlength="1" show-word-limit/>
            </el-form-item>
          </el-col>

          <!-- 网口排面相机、485 抓拍角度、方向  -->
          <el-col :span="12" v-if="form.classify != 1">
            <el-form-item label="抓拍角度" prop="paimianAngle">
              <el-input v-model="form.paimianAngle" placeholder="请输入" :max="180" maxlength="3" show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.classify != 1">
            <el-form-item label="方向">
              <el-select v-model="form.direction" placeholder="请选择">
                <el-option
                  v-for="item in subDevice_camera_direction"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <!-- 485、温控器 端口-->
          <el-col :span="12" v-if="form.classify != 2">
            <el-form-item label="使用的端口" prop="port">
              <el-select v-model="form.port" filterable placeholder="请选择">
                <el-option
                  v-for="item in subDevice_port"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>


          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入" maxlength="40" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>        

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>

import { listDeviceModel, getDeviceModelDetail, addDeviceModel, updateDeviceModel, exportDeviceModel, delDeviceModel } from "@/api/device/deviceModel";

export default {
  name: "subDeviceModel",
  data() {
    return {
      // 表单数据
      form : {
        address: null,
        classify:null,
        code: null,
        direction: null,
        model: null,
        name: null,
        paimianAngle: null,
        port: null,
        type: 2
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中项目名数组
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目表格数据
      subDeviceModelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        model: null,
        classify:null,
        code: null,
        name: null,
        remark: null,
        type:2,
        orderByColumn:'id',
        isAsc:'desc'
      },
      subDevice_camera_direction: [
        {label:'无',value:0},
        {label:'顺时针',value:1},
        {label:'逆时针',value:2},
      ],
      subDevice_classify : [
        { label: "温控器", value: 1 },
        { label: "网口排面相机", value: 2 },
        { label: "485排面相机", value: 3 },
      ],
      subDevice_port : [
        { label: "UART0", value: 0 },
        { label: "UART2", value: 1 },
        { label: "LAN0", value: 2 },
        { label: "LAN1", value: 3 },
      ],
      // 表单校验
      rules: {
        subDeviceModelId: [
          { required: true, message: "子设备型号编号不能为空", trigger: "blur" }
        ],
        subDeviceModel: [
          { required: true, message: "子设备型号不能为空", trigger: "blur" }
        ],
        subDeviceModelName: [
          { required: true, message: "子设备型号名称不能为空", trigger: "blur" }
        ],
        subDeviceModelAddress: [
          { required: true, message: "子设备地址不能为空", trigger: "blur" }
        ],
        subDeviceModelClassify: [
          { required: true, message: "子设备类别不能为空", trigger: "blur" }
        ],
        subDeviceModelPort: [
          { required: true, message: "子设备端口不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getSubDeviceModelList();
  },
  methods: {
    /** 获取项目列表 */
    getSubDeviceModelList(){
      this.loading = true;
      listDeviceModel(this.queryParams).then((res) =>{
        console.log("项目列表为",res)
        if(res.code === 200){
          this.subDeviceModelList = res.rows
          this.total = res.total
        }
      }).catch((err) =>{
        this.$message.error("子设备型号列表获取失败，原因："+err.message)
      }).finally(() =>{
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        address: null,
        classify:null,
        code: null,
        direction: null,
        model: null,
        name: null,
        paimianAngle: null,
        port: null,
        type: 2
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getSubDeviceModelList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.model)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加主设备型号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDeviceModelDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改子设备型号信息";
      });
    },
    /** 提交按钮 */
    submitForm(){
      console.log("提交的form表单为",this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDeviceModel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getSubDeviceModelList();
            });
          } else {
            addDeviceModel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getSubDeviceModelList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      //todos 修改
      const ids = row.id || this.ids;
      const names = row.model || this.names
      this.$modal.confirm('是否确认删除子设备型号名为"' + names + '"的数据项？').then(function() {
          // 修改为逻辑删除
        return delDeviceModel(ids);
      }).then(() => {
        this.getSubDeviceModelList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/genius/platform-device-model/export', {
        ...this.queryParams
      }, `subDeviceModelList_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss">
.formTitle{
  width:100%;
  text-align:center;
  margin:15px 0 15px 0;
  font-weight:border;
  font-size:20px
}
</style>
